/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swatsoft3.controlador;

import org.apache.pdfbox.util.*;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.pdfparser.*;
import org.apache.pdfbox.pdmodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;

/**
 *
 * @author andree
 */
public class BL_PDFTextParser {

    public BL_PDFTextParser() {
    }

    // Extract text from PDF Document
    public String pdftoText(String fileName) throws Exception {
        PDFParser parser;
        String parsedText = null;
        PDFTextStripper pdfStripper = null;
        PDDocument pdDoc = null;
        COSDocument cosDoc = null;

        File f = new File(fileName);

        FileInputStream fi = new FileInputStream(f);
        parser = new PDFParser(fi);

        parser.parse();
        cosDoc = parser.getDocument();
        pdfStripper = new PDFTextStripper();
        pdDoc = new PDDocument(cosDoc);
        parsedText = pdfStripper.getText(pdDoc);
        if (cosDoc != null) {
            cosDoc.close();
        }
        if (pdDoc != null) {
            pdDoc.close();
        }
        fi.close();
        return parsedText;
        
    }
    // Write the parsed text from PDF to a file

    public void writeTexttoFile(String pdfText, String fileName) throws Exception{
            PrintWriter pw = new PrintWriter(fileName);
            pw.print(pdfText);
            pw.close();            
    }
    
    public void deleteFile(String fileName) throws Exception{
            File file = new File (fileName);
            file.delete();
    }
}
